<template>
  <el-button type="text"
             @click="handleClick">{{title}}</el-button>
</template>

<script>
import axios from 'axios'
import _ from 'lodash'
export default {
  name: "WaybillCarrierCloseAgreeAction",
  inject: ["$table"],
  props: {
    record: {
      type: Object,
      default: () => { }
    },
    url: {
      type: String,
      default: null
    },
    routerName: String,
    // 按钮名称
    title: String
  },
  computed: {
    financeFlagTips () {
      return {
        1: "运单已加入对账单，不能操作",
        2: "运单已对账，不能操作",
        3: "运单已结账，不能操作"
      }
    },
    financePayTips () {
      return {
        1: "已收",
        2: "已付",
        3: "已收已付"
      }
    }
  },
  methods: {
    handleClick: _.debounce(function () {    // 触发事件
      if (this.record.finance?.prepayFeeFlag) {
        return this.$message({ type: 'warning', message: `运单${this.financePayTips[this.record.finance?.prepayFeeFlag]}预付款，不能操作` });
      } else if (this.record.finance?.arrivePayFeeFlag) {
        return this.$message({ type: 'warning', message: `运单${this.financePayTips[this.record.finance?.arrivePayFeeFlag]}货到付款，不能操作` });
      } else if (this.record.finance?.collectionFeeFlag) {
        return this.$message({ type: 'warning', message: `运单${this.financePayTips[this.record.finance?.collectionFeeFlag]}代收货款，不能操作` });
      }

      if (this.record?.finance?.financeFlag) {
        return this.$message({ type: 'warning', message: this.financeFlagTips[this.record.finance?.financeFlag] });
      }

      this.$confirm("确认同意关单？", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.closeWaybill();
      });
    }, 600),
    async closeWaybill () {
      let response = await axios.post(this.url, { waybillId: this.record?.waybillId || this.record?.id });
      if (response.code === 0) {
        // 刷列表 
        this.$table.search();
      } else if (response.code === -10341) {
        this.$confirm(`${response.message}`, "提示", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.goDetail()
        });
      } else {
        this.$message({ type: "error", message: response.message });
      }
    },
    goDetail () {
      this.$router.push({
        name: this.record?.combineStatus === 10 ? 'oneBill' : 'oneBillDetails',
        query: {
          combineId: this.record.combineId
        }
      });
    },
  }
}
</script>
<style scoped lang="scss">
.arrow {
  color: var(--prev-color-text-regular);
  font-size: 13px;
  padding: 0;
}
</style>